package com.base.service.impl;

import java.util.List;

import com.base.service.FlowtemplatedetailService;
import com.common.bean.Flowtemplatedetail;
import com.common.util.AbstractManager;
import com.common.util.PagerModel;

public class FlowtemplatedetailServiceImpl extends AbstractManager implements
		FlowtemplatedetailService {

	public void addFlowtemplatedetail(Flowtemplatedetail flowtemplatedetail) {
		String hql = "select nvl(max(workflowid),0) from Flowtemplatedetail";
		
		long workflowid = 0l;
		workflowid = (Long) this.getHibernateTemplate().find(hql).get(0);
		workflowid += 1l;
		
		flowtemplatedetail.setWorkflowid(workflowid);
		this.getHibernateTemplate().save(flowtemplatedetail);
	}

	@SuppressWarnings("unchecked")
	public List<Flowtemplatedetail> findAllFlowtemplatedetail() {
		String hql = "from Flowtemplatedetail order by workflowid";
		return this.getHibernateTemplate().find(hql);
	}
	
	public List findAllFlowtemplatedetailWith(long workflowno) {
		String hql = "select detail.workflowid,detail.seq,detail.datadictByWorkflowstatusno.name," +
				"detail.department.deptcaption,detail.rule.rulename,detail.datadictByBranch.name " +
				"from Flowtemplatedetail detail where detail.flowworkitem.workflowno = ? order by detail.workflowid";
		return this.getHibernateTemplate().find(hql,workflowno);
	}

	public Flowtemplatedetail findFlowtemplatedetail(long workflowid) {
		return (Flowtemplatedetail) this.getHibernateTemplate().load(Flowtemplatedetail.class, workflowid);
	}

	public PagerModel findFlowtemplatedetailForPager() {
		String hql = "from Flowtemplatedetail order by workflowid, seq";
		return searchPaginated(hql);
	}

	public void removeFlowtemplatedetail(long workflowid) {
		Flowtemplatedetail flowtemplatedetail = null;
		flowtemplatedetail = (Flowtemplatedetail) this.getHibernateTemplate().load(Flowtemplatedetail.class, workflowid);
		if(flowtemplatedetail != null) {
			this.getHibernateTemplate().delete(flowtemplatedetail);
		}
	}

	public void updateFlowtemplatedetail(Flowtemplatedetail flowtemplatedetail) {
		this.getHibernateTemplate().update(flowtemplatedetail);
	}
}
